rednet.open("left")

local frameCoords = {}
frameCoords["x"] = 0
frameCoords["y"] = 0
frameCoords["z"] = 0

local directions = {}

local seaLevelY = 80
local frameCompID = 6
local moveDelay = 0.5
local stop = false

local autoMode = false
local domeActive = false
local cycleActive = false
local rsColor = 0


local currentFunction = ""

function setDirections()
	directions["Forward"] = colors.brown
	directions["Backward"] = colors.blue
	directions["Up"] = colors.green
	directions["Down"] = colors.red
end

function getFrameLoc()
	local data = {}
	local id,msg,proto
	repeat
		rednet.send(frameCompID, "GPS")
		id,msg,proto = rednet.receive(0.1)
		if msg then
			data = textutils.unserialize(msg)
			if data[0] == "CurrentFunction" then
				sendCurrentFunction(id)
			elseif data[0] == "STOP" then
				print("GetLoc - Stopping")
				stop = true
			end
		end
	until data["x"]
	
	return data["x"], data["y"], data["z"]
end

function setFrameCoords()
	frameCoords["x"], frameCoords["y"], frameCoords["z"] = getFrameLoc()
end

function checkMove()
	local x,y,z = getFrameLoc()
	if frameCoords["x"] == x and frameCoords["y"] == y and frameCoords["z"] == z then
		return false
	else
		return true
	end
end

function sendItems()
	currentFunction = "SendItems"
	pulseWire(colors.purple, 2)
	currentFunction = ""
end

function pulseWire(color, delay)
	rsColor = rsColor+color
	rs.setBundledOutput("right", rsColor)
	checkMessageStop(delay)
	rsColor = rsColor-color
	rs.setBundledOutput("right", rsColor)
end

function moveFrame(direction)
	currentFunction = direction
	repeat
		pulseWire(directions[direction], 0.5)
	until checkMove()
	setFrameCoords()
	currentFunction = ""
end

function moveFrameNoCheck(direction)
	pulseWire(directions[direction], 0.5)
end

function checkMessageStop(delay)
	local timerCode = os.startTimer(delay)
	local event,id,msg,proto
	repeat
		event,id,msg,proto = os.pullEvent()
		if event=="rednet_message" then
			data = textutils.unserialize(msg)
			if data[0] == "STOP" then
				print("checkStop - Stopping")
				stop = true
			elseif data[0] == "CurrentFunction" then
				sendCurrentFunction(id)
			end
		end
	until id == timerCode
	return false
end

function sendCurrentFunction(id)
	local data = {}
	print("Current Function: "..currentFunction)
	data[0] = currentFunction
	data[1] = autoMode
	data[2] = cycleActive
	rednet.send(id,textutils.serialize(data))
end

function toY(level)
	while frameCoords["y"] ~= level do
		if stop then 
			autoMode = false
			break 
		end
		print(frameCoords["y"])
		if level < frameCoords["y"] then
			moveFrameNoCheck("Down")
		else
			moveFrameNoCheck("Up")
		end
		setFrameCoords()
	end
	
end

function toBedrock()
	currentFunction = "Bedrock"
	toY(6)
	currentFunction = ""
end

function toSky()
	currentFunction = "SeaLevel"
	toY(seaLevelY)
	currentFunction = ""
end

function mineCycle()
	cycleActive = true
	toBedrock()
	sleep(2)
	toSky()
	cycleActive = false
end

function nextCycle()
	cycleActive = true
	toSky()
	sendItems()
	moveAhead(8)
	cycleActive = false
end

function moveAhead(times)
	if stop then 
		autoMode = false
		return false
	end
	for i = 1,times do
		moveFrame("Forward")
	end
end

function printCoords()
	setFrameCoords()
	print(frameCoords["x"]..", "..frameCoords["y"]..", "..frameCoords["z"])
end

function sendComplete(id)
	rednet.send(id, "Complete")
end

function autoMine()
	mineCycle()
	sendItems()
	moveAhead(8)
end

function dome()
	if domeActive then
		rsColor = 0
	else
		rsColor = colors.cyan
	end
	rs.setBundledOutput("right", rsColor)
end

function setDomeActive(active)
	domeActive = active
end

function setAutoMode(mode)
	autoMode = mode
end

function getmsg()
	--dome()
	setFrameCoords()
	stop = false
	if autoMode then
		autoMine()
	else
		local data = {}
		local id, message, distance, protocol = rednet.receive()
		print("MainFunct: "..message)
		data = textutils.unserialize(message)
		if data[0] == "moveFrame" then
			moveFrame(data[1])
			setFrameCoords()
			currentFunction = ""
			--sendComplete(id)
		elseif data[0] == "Bedrock" then
			toBedrock()
		elseif data[0] == "SeaLevel" then
			toSky()
		elseif data[0] == "SendItems" then
			sendItems()
		elseif data[0] == "CurrentFunction" then
			sendCurrentFunction(id)
		elseif data[0] == "AutoMode" then
			setAutoMode(true)
		elseif data[0] == "Dome" then
			setDomeActive(not domeActive)
		elseif data[0] == "Cycle" then
			autoMine()
		elseif data[0] == "NextCycle" then
			nextCycle()
		end
	end
end


printCoords()
setDirections()
while true do
	getmsg()
end
